package com.newly.driver.sdk.device.video.service.impl.tencent;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.newly.driver.sdk.device.video.cloud.record.QueryRecordReq;
import com.newly.driver.sdk.device.video.cloud.record.QueryRecordResp;
import com.newly.driver.sdk.device.video.cloud.record.RecordItem;
import com.newly.driver.sdk.device.video.cloud.record.tencent.*;
import com.newly.driver.sdk.device.video.exception.NewlyDeviceException;
import com.newly.driver.sdk.device.video.service.api.tencent.ITencentRecord;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.DeviceItem;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.*;

/**
 * @author gjm guan
 * @date 2021/10/27 11:23
 * @desc 腾讯云录制实现
 */
public class TencentRecord extends AbstractTencentCloud implements ITencentRecord {

    @Override
    public boolean startRecord(int startTime, int stopTime, String deviceId, String channelId) {
        try {
            Assert.notBlank(deviceId, "deviceId不能为空");
            Assert.notBlank(channelId, "channelId不能为空");
        } catch (IllegalArgumentException e) {
            throw new NewlyDeviceException(e.getMessage());
        }
        String recordId = this.getRecordId(deviceId, channelId);
        TXTimeTemplateItem fullTimeTemplate = this.getFullTimeTemplate();
        String timeTemplateId = fullTimeTemplate.getTemplateId();

        // 全时间段录制
        CreateRecordPlanRequest req = new CreateRecordPlanRequest();
        // 录制计划名
        req.setName(recordId);
        // 录制设备信息
        DeviceItem deviceItem = new DeviceItem();
        deviceItem.setDeviceId(deviceId);
        deviceItem.setChannelId(channelId);
        DeviceItem[] deviceItems = ArrayUtil.newArray(DeviceItem.class, 1);
        deviceItems[0] = deviceItem;
        req.setDevices(deviceItems);
        // 时间模板id
        req.setTimeTemplateId(timeTemplateId);
        // 录制时间
        req.setEventId(1l);

        CreateRecordPlanResponse resp = null;
        try {
            resp = this.httpClient().CreateRecordPlan(req);
            String planId = resp.getPlanId();
            if (!StrUtil.isBlank(planId)) {
                recordingItems.put(recordId, new RecordItem(recordId, resp.getPlanId(), deviceId, channelId, startTime, stopTime));
                return true;
            }
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean startRecord(String deviceId, String channelId) {
        return this.startRecord(0, 0, deviceId, channelId);
    }

    @Override
    public boolean stopRecord(String deviceId, String channelId) {
        // 从recordingItems录制项，如果找不到则认为录制失效
        String recordId = this.getRecordId(deviceId, channelId);
        RecordItem recordItem = this.recordingItems.get(recordId);
        if (ObjectUtil.isEmpty(recordItem) || StrUtil.isEmpty(recordItem.getPlantId())) {
            return true;
        }

        DeleteRecordPlanRequest req = new DeleteRecordPlanRequest();
        req.setPlanId(recordItem.getPlantId());
        DeleteRecordPlanResponse resp = null;
        try {
            resp = this.httpClient().DeleteRecordPlan(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return TencentRequestState.OK.getValue().equals(resp.getStatus());
    }

    @Override
    public QueryRecordResp queryRecord(QueryRecordReq queryRecordReq) {
        try {
            Assert.notNull(queryRecordReq, "录制请求参数异常");
            Assert.notBlank(queryRecordReq.getDeviceId(), "deviceId不能为空");
            Assert.notBlank(queryRecordReq.getChannelId(), "channelId不能为空");
        } catch (IllegalArgumentException e) {
            throw new NewlyDeviceException(e.getMessage());
        }
        GetVideoListByConRequest req = new GetVideoListByConRequest();
        req.setDeviceId(queryRecordReq.getDeviceId());
        //req.setChannelId(queryRecordReq.getChannelId());
        req.setOffset(1L);
        req.setLimit(10L);
        try {
            GetVideoListByConResponse resp = this.httpClient().GetVideoListByCon(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public QueryRecordResp queryRecord(String deviceId, String channelId) {
        return this.queryRecord(new QueryRecordReq(deviceId, channelId));
    }

    @Override
    public TXCreateTimeTempResp createTimeTemplate(TXCreateTimeTempReq txCreateTimeTempReq) {
        try {
            Assert.notNull(txCreateTimeTempReq, "创建时间模板请求参数异常");
            Assert.notBlank(txCreateTimeTempReq.getName(), "name不能为空");
            Assert.notNull(txCreateTimeTempReq.getIsFoolTime(), "isFoolTime不能为空");
        } catch (IllegalArgumentException e) {
            throw new NewlyDeviceException(e.getMessage());
        }

        CreateTimeTemplateRequest req = new CreateTimeTemplateRequest();
        BeanUtil.copyProperties(txCreateTimeTempReq, req);

        CreateTimeTemplateResponse resp = null;
        try {
            resp = this.httpClient().CreateTimeTemplate(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }

        return new TXCreateTimeTempResp(resp);
    }

    @Override
    public TXQueryTimeTempResp queryTimeTemplate() {
        GetTimeTemplatesResponse resp = null;
        try {
            resp = this.httpClient().GetTimeTemplates(new GetTimeTemplatesRequest());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return new TXQueryTimeTempResp(resp);
    }

    @Override
    public TXCreateRecordPlanResp createRecordPlan(TXCreateRecordPlanReq txCreateRecordPlanReq) {
        try {
            Assert.notNull(txCreateRecordPlanReq, "创建录制计划数据异常");
            Assert.notBlank(txCreateRecordPlanReq.getName(), "name不能为空");
            Assert.notBlank(txCreateRecordPlanReq.getTimeTempId(), "timeTempId不能为空");
            Assert.notBlank(txCreateRecordPlanReq.getEventId(), "eventId不能为空");
            Assert.notEmpty(txCreateRecordPlanReq.getDeviceItems(), "deviceItems不能为空");
        } catch (IllegalArgumentException e) {
            throw new NewlyDeviceException(e.getMessage());
        }

        CreateRecordPlanRequest req = new CreateRecordPlanRequest();
        BeanUtil.copyProperties(txCreateRecordPlanReq, req);

        CreateRecordPlanResponse resp = null;
        try {
            resp = this.httpClient().CreateRecordPlan(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }

        return new TXCreateRecordPlanResp(resp);
    }
}
